<?php

namespace App\Http\Controllers\Api\System;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;

class AdminLogic extends Controller
{
    /**
     * 设置角色权限
     */
    public function assignRoles(Request $request)
    {
        $this->validate($request,[
            'title' => 'required',
            'perm' => 'required',
        ]);
        Permission::updateOrcreate([
            'name' => $request->input('perm'),
            'desc' => $request->input('title')
        ]);
        $role = Role::find($request->input('role_id'));
        if($role)
        {
             $role->givePermissionTo(explode(',', $request->input('perm')));
        }
        return $this->success(['message' => '设置成功']);
    }

    //取消角色权限
    public function syncPermissionsRoles(Request $request)
    {
         $this->validate($request,[
            'role_id' => 'required',
            'perm' => 'required'
        ]);

        $role = Role::find($request->input('role_id'));
        if($role)
        {
             $role->revokePermissionTo(explode(',', $request->input('perm')));
            return $this->success(['message' => '删除成功']);
        }
        return $this->error('删除失败');
    }
}
